package us.icitap.cm.ui.policecase;

import java.util.List;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.tapio.googlemaps.GoogleMap;
import com.vaadin.tapio.googlemaps.client.LatLon;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Place;
import us.icitap.cm.business.entities.regmin.Cities;
import us.icitap.cm.business.entities.regmin.District;
import us.icitap.cm.business.entities.regmin.Region;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustHorizontalLayout;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.ui.shared.MapComponent;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class PlaceDataLayout extends AppEntityLayout{

	Place place;
	private final CustBeanFieldGroup<Place> binder = new CustBeanFieldGroup<Place>(Place.class);
	protected AppEntityListener listener;
	private final CustComboBox streetSelect = new CustComboBox("Rruga");
	private final CustTextField numberTextField = new CustTextField("Numri");
	private final CustTextField apartmentTextField = new CustTextField("Apartamenti");
	private final CustComboBox stateSelect = new CustComboBox("Shteti");
	private final CustComboBox citySelect = new CustComboBox("Qyteti");
	private final CustComboBox regionSelect = new CustComboBox("Qarku");
	private final CustComboBox districtSelect = new CustComboBox("Rrethi");
	
	private final CustComboBox placeClassificationSelect = new CustComboBox("Klasifikimi i vendit/objektit");
	private final CustComboBox placeTypeSelect = new CustComboBox("Tipi i vendit/objektit");
	
	
	private final CustTextArea addressTextArea = new CustTextArea("Shenime");
	final CustomFormLayout cfl = new CustomFormLayout("450px", "Vend/Adrese", "Reset", "Ruaj");
	
	boolean editableVisible = false;
	boolean readOnlyVisible = false;

	final TabSheet tabSheet = new TabSheet();
	
	
	private final MapComponent map;

	public PlaceDataLayout() {
		this.setMargin(true);
		
		
		
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		
		fl.addComponent(this.placeClassificationSelect);
		fl.addComponent(this.placeTypeSelect);
		fl.addComponent(this.streetSelect);
		fl.addComponent(this.numberTextField);
		fl.addComponent(this.apartmentTextField);
		fl.addComponent(this.stateSelect);
		fl.addComponent(this.citySelect);
		fl.addComponent(this.regionSelect);
		fl.addComponent(this.districtSelect);
		fl.addComponent(this.addressTextArea);
		
		tabSheet.addTab(fl, "Te dhenat");
		
		final GoogleMap googleMap = new GoogleMap("AIzaSyAW71WMwuWuoxZd9Fl3cy0BnkbH-NqjhSY");
		map = new MapComponent("450px", true, googleMap);
		
		tabSheet.addTab(map, "Harta");
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		numberTextField.setWidth("6em");
		addressTextArea.setWidth("20em");
		addressTextArea.setRows(4);
		
		stateSelect.setContainerDataSource(new BeanItemContainer<States>(States.class, ProjectLists.getStatesList(false)));
		stateSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		stateSelect.setItemCaptionPropertyId("tag");
		
		citySelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		citySelect.setItemCaptionPropertyId("tag");
		
		regionSelect.setContainerDataSource(new BeanItemContainer<Region>(Region.class, ProjectLists.getRegionList(false)));
		regionSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		regionSelect.setItemCaptionPropertyId("tag");
		
		districtSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		districtSelect.setItemCaptionPropertyId("tag");
		
		placeClassificationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PlaceClassification, false)));
		placeClassificationSelect.setImmediate(true);
		
		placeTypeSelect.setImmediate(true);
		
		stateSelect.setImmediate(true);
		stateSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				citySelect.removeAllItems();
				if(stateSelect.getValue() != null){
					Result<List<Cities>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getCities(stateSelect.getValue());
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						citySelect.setContainerDataSource(new BeanItemContainer<Cities>(Cities.class, result.getObject()));
						
					}
				}
				
				
			}
		});
		
		regionSelect.setImmediate(true);
		regionSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				districtSelect.removeAllItems();
				if(regionSelect.getValue() != null){
					Result<List<District>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getDistrict(regionSelect.getValue());
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						districtSelect.setContainerDataSource(new BeanItemContainer<District>(District.class, result.getObject()));
						
					}
				}
				
			}
		});
		
		citySelect.setImmediate(true);
		citySelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(citySelect.getValue() != null){
					Cities city = (Cities) citySelect.getValue();
					if(city.getDistrict()!= null){
						regionSelect.setValue(city.getDistrict().getRegion());
						districtSelect.setValue(city.getDistrict());
						addressTextArea.focus();
					}
				}
				
			}
		});
		
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					if(PlaceDataLayout.this.listener != null){
						if(oldAppEntity == null){
							System.out.println(">1");
							PlaceDataLayout.this.listener.addAppEntityToReport(place.getAppEntity());
						}
						else
							if(!oldAppEntity.getPlace().myEquals(place)){
								System.out.println(">2");
								place.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								PlaceDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, place.getAppEntity());
							}
							else
							{
								
								
//								oldAppEntity.replaceEntityReportAssociationList(place.getAppEntity().getReportAssociationList());
								
								System.out.println(">3");
								PlaceDataLayout.this.listener.refresh(oldAppEntity);
							}
						
						addAppEntity();
						
					}
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});

		
		placeClassificationSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				placeTypeSelect.removeAllItems();
				if(placeClassificationSelect.getValue() != null){
					AppList parent = (AppList) placeClassificationSelect.getValue();
					Result<List<AppList>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAppListChildren(parent);
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						placeTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, result.getObject()));
						
					}
				}
				
				
			}
		});
		
		
		binder.bind(placeClassificationSelect, "placeClassification");
		binder.bind(placeTypeSelect, "placeType");
		binder.bind(streetSelect, "street");
		binder.bind(numberTextField, "number");
		binder.bind(apartmentTextField, "apartment");
		binder.bind(stateSelect, "states");
		binder.bind(citySelect, "cities");
		binder.bind(districtSelect, "district");
		binder.bind(regionSelect, "region");
		binder.bind(addressTextArea, "addressText");
		
		
	}
	

	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}
	

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = ViewCaseLayout.showPlace(place);
		vl.setCaption("Vend/Adrese");
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	@Override
	public void addAppEntity() {
		
		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.PLACE);

		place = appEntity.getPlace();
		binder.setItemDataSource(place);
	
		this.showEditableForm();

	}
	
	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}

	private AppEntity oldAppEntity;
	
	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {
		System.out.println("PlaceDataLayout.loadAppEntity()");
		oldAppEntity = appEntity;
		
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		this.place = newAppEntity.getPlace();
		binder.setItemDataSource(place);

		if(editable)
			this.showEditableForm();
		
		else 
			this.showReadOnlyForm();
	
	}


}
